﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Data;

public partial class admin_Order_List : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      ShowData();
    }
  }
  OrderBLL orderData = new OrderBLL();
  OrderDetailsBLL orderDetaildata = new OrderDetailsBLL();
  private void ShowData()
  {
      int PageRow = Convert.ToInt32(dropPageSize.SelectedValue);
      gridView.PageSize = PageRow;
    bool status = false;
    List<Order> list = new List<Order>();
    if (dropIsView.SelectedIndex > 0)
    {
      status = Convert.ToBoolean(dropIsView.SelectedValue);
      list = orderData.GetByStatus(status);
    }
    else
    {
      list = orderData.GetAll();
    }
    gridView.DataSource = ToDataSoure(list);
    gridView.DataBind();
  }

  private object ToDataSoure(List<Order> list)
  {
    UserBLL userData = new UserBLL();
    DataTable table = new DataTable();
    table.Columns.Add("Id");
    table.Columns.Add("IdCustomer");
    table.Columns.Add("Customer");
    table.Columns.Add("AddressGetProduct");
    table.Columns.Add("DateSend");
    table.Columns.Add("Status");
    foreach (var item in list)
    {
      DataRow row = table.NewRow();
      row["Id"] = item.Id.ToString();
      row["IdCustomer"] = item.IdCustomer.ToString();
      if (item.IdCustomer != null)
      {
        try { row["Customer"] = userData.GetById(item.IdCustomer.Value).Name; }
        catch { }
      }
      else
      {
        row["Customer"] = item.Name;
      }
      row["AddressGetProduct"] = item.AddressGetProduct;
      row["DateSend"] = item.DateSend.ToString("HH:mm d/M/yyyy");
      row["Status"] = (item.Status == true ? "Đã duyệt" : "Chưa duyệt");
      table.Rows.Add(row);
    }
    return table;
  }
  public void ResetForm()
  {

  }
  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    lbThongBao.Text = "";
    try
    {
      lbThongBao.Text = "";
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  }
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);

      orderDetaildata.DeleteByOrderId(id);
      orderData.Delete(id);
      ResetForm();
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[1].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[2].Text);
              orderDetaildata.DeleteByOrderId(id);
              orderData.Delete(id);
            }
            break;
          }
        }
      }
      ResetForm();
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void dropIsView_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void dropSort_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      int id = Convert.ToInt32(e.Row.Cells[2].Text);
      Order order = orderData.GetById(id);
      if (!order.IsRead)
      {
        e.Row.BackColor = System.Drawing.Color.FromName("#628199");
        e.Row.ForeColor = System.Drawing.Color.White;
      }
    }
    catch
    {

    }
  }
  protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int index = Convert.ToInt32(e.CommandArgument);
      int id = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
      Order obj = orderData.GetById(id);
      //ltAddress.Text = obj.Address;
      //ltContent.Text = HttpUtility.HtmlDecode(obj.Content);
      //ltDateSend.Text = obj.DateSend.ToString();
      //ltEmail.Text = obj.Email;
      //ltName.Text = obj.Name;
      //ltPhone.Text = obj.Phone;
      //ltTitle.Text = obj.Title;

      orderData.Read(id);
    }
    catch
    {

    }
  }
  protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
    ShowData();
  }
}